package com.hbx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbx.auth.AuthCacheService;
import com.hbx.constant.CachePrefixContent;
import com.hbx.exception.RspException;
import com.hbx.model.PkResult;
import com.hbx.mapper.PkResultMapper;
import com.hbx.model.User;
import com.hbx.service.PkResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.utils.CacheUtils;
import com.hbx.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbx
 * @since 2022-01-29
 */
@Service
public class PkResultServiceImpl extends ServiceImpl<PkResultMapper, PkResult> implements PkResultService {

    @Autowired
    private PkResultMapper resultMapper;

    @Autowired
    private CacheUtils cacheUtils;

    /**
     * 发表意见
     * @param result
     * @param r
     */
    @Override
    public void addPkResult(PkResult result, Result r) {
        Integer[] integers = new Integer[]{1,2};
        if (!Arrays.asList(integers).contains(result.getResultType())){
            r.setCode(201).setMsg("类型暂不支持");
            return;
        }
        User user = AuthCacheService.getUser();
        QueryWrapper<PkResult> wrapper = new QueryWrapper<>();
        wrapper.eq("result_user_id", user.getUserId());
        wrapper.eq("pk_id", result.getPkId());
        List<PkResult> results = resultMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(results)){
            r.setCode(201).setMsg("您已发表过见解,请勿重复发表");
            return;
        }
        result.setResultUserId(user.getUserId());
        result.setResultPublishDate(new Date());
        int insert = resultMapper.insert(result);
        if (insert != 1){
            throw new RspException(201, "发表失败");
        }
        cacheUtils.add(CachePrefixContent.PLAT_PK_RESULT_NIU_INFO + result.getId(), new Object[]{-1});
        r.setCode(200).setMsg("发表成功");
    }

    /**
     * 支持与取消支持
     * @param id
     * @param r
     */
    @Override
    public void addResultNiu(Integer id, Result r) {
        PkResult pkResult = resultMapper.selectById(id);
        User user = AuthCacheService.getUser();
        if (pkResult == null){
            r.setCode(201).setMsg("操作失败,观点不存在");
            return;
        }
        Set<Object> members = cacheUtils.getSetMembers(CachePrefixContent.PLAT_PK_RESULT_NIU_INFO + id);
        if (CollectionUtils.isEmpty(members)){
            // 直接支持
            Integer integer = resultMapper.updateNiu(id, 1);
            if(integer == 1){
                cacheUtils.add(CachePrefixContent.PLAT_PK_RESULT_NIU_INFO + id, new Object[]{user.getUserId()});
                r.setCode(200).setMsg("支持成功");
            } else {
                throw new RspException(201,"支持失败");
            }
        } else {
            if (members.contains(user.getUserId())){
                // 取消支持
                Integer integer = resultMapper.updateNiu(id, 0);
                if(integer == 1){
                    cacheUtils.setRemove(CachePrefixContent.PLAT_PK_RESULT_NIU_INFO + id, new Object[]{user.getUserId()});
                    r.setCode(200).setMsg("取消支持成功");
                } else {
                    throw new RspException(201,"取消支持失败");
                }
            } else {
                // 支持
                Integer integer = resultMapper.updateNiu(id, 1);
                if(integer == 1){
                    cacheUtils.add(CachePrefixContent.PLAT_PK_RESULT_NIU_INFO + id, new Object[]{user.getUserId()});
                    r.setCode(200).setMsg("支持成功");
                } else {
                    throw new RspException(201,"支持失败");
                }
            }
        }
    }

    /**
     * 删除观点
     * @param id
     * @param r
     */
    @Override
    public void removeIdea(Integer id, Result r) {
        PkResult pkResult = resultMapper.selectById(id);
        if (pkResult == null){
            r.setCode(201).setMsg("操作失败,观点不存在");
            return;
        }
        int result = resultMapper.deleteById(id);
        if (result != 1){
            r.setCode(201).setMsg("删除失败");
            return;
        }
        if (cacheUtils.removeKey(CachePrefixContent.PLAT_PK_RESULT_NIU_INFO + id)){
            r.setCode(200).setMsg("删除成功");
            return;
        }
        r.setCode(201).setMsg("删除失败");
    }
}
